<template>
	<div class="system-user-container layout-padding">
		<div class="search-box">
			<SearchForm :listQuery="state.listQuery" :formOption="state.formOption" @onSearch="onSearch" @resetSearch="resetSearch"></SearchForm>
		</div>
		<el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
				<el-button size="default" type="primary" @click="onOpenAddUser('add')">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增
				</el-button>
			</div>
			<el-table :data="state.tableData" v-loading="state.loading" border>
				<el-table-column type="index" label="序号" width="80" align="center"/>
				<el-table-column prop="name" label="用户名称"></el-table-column>
				<el-table-column prop="status" label="用户状态">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 'normal'">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="created_at" label="创建时间"></el-table-column>
				<el-table-column label="操作" width="250">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditUser('edit', scope.row.id)" v-if="scope.row.name !== 'admin'"
							>修改</el-button
						>
            <el-button size="small" text type="primary" @click="onOpenEditUser('pwd', scope.row.id)"
							>修改密码</el-button
						>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-if="scope.row.name !== 'admin'">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.listQuery.page"
				background
				v-model:page-size="state.listQuery.size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.totalCount"
			>
			</el-pagination>
		</el-card>
		<UserDialog ref="userDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { adminList, deleteAdmin } from '../../../api/admin';

// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/system/user/dialog.vue'));
// 搜索组件
const SearchForm = defineAsyncComponent(() => import('/@/components/searchForm/index.vue'));

// 定义变量内容
const userDialogRef = ref();
const state = reactive({
	listQuery: {
		page: 1,
		size: 10,
	},
	clearForm: {
		page: 1,
		size: 10,
	},
	formOption: [
		{
			label: '用户名',
			prop: 'keywords',
			element: 'input',
			option: {
				maxlength: 50,
			},
		},
		{
			label: '状态',
			prop: 'status',
			element: 'select',
			selectOption: [
				{ label: '启用', value: 'normal' },
				{ label: '禁用', value: 'disable' },
			],
		},
	],
	tableData: [],
	totalCount: 0,
	loading: false,
});

// 初始化表格数据
const getTableData = () => {
	state.loading = true;
	adminList(state.listQuery).then((res: any) => {
		if (res.status === 'success') {
			state.tableData = res.data.data;
			state.totalCount = res.data.total;
		}
	});
	setTimeout(() => {
		state.loading = false;
	}, 500);
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
	userDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, id: number) => {
	userDialogRef.value.openDialog(type, id);
};
// 删除用户
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.userName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
      deleteAdmin(row.id).then((res: any) => {
				if (res.status === 'success') {
					ElMessage.success('删除成功');
					getTableData();
				} else {
					ElMessage.error(res.msg);
				}
			});
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.listQuery.size = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.listQuery.page = val;
	getTableData();
};
// 搜索
const onSearch = () => {
	getTableData();
};

// 重置
const resetSearch = () => {
	state.listQuery = JSON.parse(JSON.stringify(state.clearForm));
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
